#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/13  12:52
# @Author: 余浪人
# @email: yulangren520@gmail.com
import os
from flask import request, jsonify, current_app
from flask_login import login_required
from apps.control import sys_bp
from apps import get_sys_url
from apps.lib.public import get_uuid16, get_date_str
from qiniu import Auth, put_data
from apps.models.system_model import TL_Save_Model


def qiNiu_upload (OO00000000OO0OO0O :str ,O00O00000O00O0O00 ):
    try :
        O0O0OOO0OOOOOO00O =TL_Save_Model .query .first ()
        O0O0OO000OO0OOO0O =Auth (O0O0OOO0OOOOOO00O .access_key ,O0O0OOO0OOOOOO00O .secret_key )
        _O00O0OOO0OOOOO00O =O0O0OOO0OOOOOO00O .save_path [1 :]if O0O0OOO0OOOOOO00O .save_path [0 ]=='/'else O0O0OOO0OOOOOO00O .save_path
        _O00O0OOO0OOOOO00O =_O00O0OOO0OOOOO00O if _O00O0OOO0OOOOO00O [-1 ]=='/'else _O00O0OOO0OOOOO00O +'/'
        OOO00OOOO00OO0OOO =os .path .join (_O00O0OOO0OOOOO00O ,OO00000000OO0OO0O )
        OO0OOOOOO0OO00O0O =O0O0OO000OO0OOO0O .upload_token (O0O0OOO0OOOOOO00O .bucket_name ,OOO00OOOO00OO0OOO ,3600 )
        O0OO0O00OO0OOO0OO ,OO0O0OOO00O000O00 =put_data (OO0OOOOOO0OO00O0O ,OOO00OOOO00OO0OOO ,O00O00000O00O0O00 )
        assert O0OO0O00OO0OOO0OO ['key']==OOO00OOOO00OO0OOO
        OOOOOO0O0O0OO0O0O =O0O0OOO0OOOOOO00O .bucket_domain if O0O0OOO0OOOOOO00O .bucket_domain [-1 ]=='/'else O0O0OOO0OOOOOO00O .bucket_domain +'/'
        return OOOOOO0O0O0OO0O0O +OOO00OOOO00OO0OOO
    except :return None
@sys_bp .route ('/upload',endpoint ='upload',methods =['POST'])#line:2
@login_required
def upload_file ():
    OO00O000O0O0OOOO0 =''
    OO0O000O0OO00O0OO =['gif','jpg','jpeg','png','bmp']
    OOO0O000OOOO0O00O =['swf','flv']
    OO0O0OOOOOOOO0OO0 =['swf','flv','mp3','wav','wma','wmv','mid','avi','mpg','asf','rm','rmvb']
    O00O0O0O00O0OOOOO =['doc','docx','xls','xlsx','ppt','htm','html','txt','zip','rar','gz','bz2']
    OO0OOOOOO00O000O0 =request .args .get ('dir')
    if request .method =='POST':
        OO00O00O0000O000O =request .files ['imgFile']
        OOO0OOOOO000OO0O0 ,O000O000000OOO0O0 =os .path .splitext (OO00O00O0000O000O .filename )
        O0OO0000OOO0OOOO0 ='%s%s'%(get_uuid16 (),O000O000000OOO0O0 )
        if OO0OOOOOO00O000O0 =='image':
            if O000O000000OOO0O0 .replace ('.','').lower ()in OO0O000O0OO00O0OO :
                OO00O000O0O0OOOO0 ="images/"
        elif OO0OOOOOO00O000O0 =='flash':
            if O000O000000OOO0O0 .replace ('.','').lower ()in OOO0O000OOOO0O00O :
                OO00O000O0O0OOOO0 ="flash/"
        elif OO0OOOOOO00O000O0 =='media':
            if O000O000000OOO0O0 .replace ('.','').lower ()in OO0O0OOOOOOOO0OO0 :
                OO00O000O0O0OOOO0 ="medias/"
        else :
            if O000O000000OOO0O0 .replace ('.','').lower ()in O00O0O0O00O0OOOOO :
                OO00O000O0O0OOOO0 ="files/"
        if OO00O000O0O0OOOO0 :
            OOO00O00OO00OO0OO =os .path .join (OO00O000O0O0OOOO0 ,get_date_str (),O0OO0000OOO0OOOO0 ).replace ('\\','/')
            _O00O0O0O000000OO0 =qiNiu_upload (OOO00O00OO00OO0OO ,OO00O00O0000O000O .read ())
            if _O00O0O0O000000OO0 :return jsonify ({"error":0 ,"url":_O00O0O0O000000OO0 ,'message':'上传成功!'})
            O0O0O000OOO000OO0 ='上传异常,请确认七牛云配置正确!'
            return jsonify ({"error":1 ,"msg":O0O0O000OOO000OO0 ,'message':O0O0O000OOO000OO0 })
        return jsonify ({"status":1 ,"message":'上传失败,超出允许文件类型!'})
    return jsonify ({"error":1 ,"msg":"上传错误!",'message':'上传错误!'})
@sys_bp .route ('/upload_img',endpoint ='upload_img',methods =['POST'])
@login_required
def upload ():
    O000O0OOOOO000O00 =['png','jpg','jpeg','bmp','gif','ico']
    if request .method =='POST':
        OOO0OO0O000O0O0OO =request .files ['file']
        OO0O000OOOOO0O00O ,OOO000OOOO0O00OOO =os .path .splitext (OOO0OO0O000O0O0OO .filename )
        O0O0OO00OOOO0OOOO ='%s%s'%(get_uuid16 (),OOO000OOOO0O00OOO .lower ())
        if OOO000OOOO0O00OOO .replace ('.','').lower ()in O000O0OOOOO000O00 :
            O0O0OOO000000O000 ="images/"
            O0O00OO0OOO000O00 =os .path .join (O0O0OOO000000O000 ,get_date_str (),O0O0OO00OOOO0OOOO ).replace ('\\','/')
            if OOO000OOOO0O00OOO .replace ('.','').lower ()in 'ico':
                O0O00OOOOO0OO0OOO =get_sys_url ()+'/apps/static'#
                O0O00OOO0O000O000 =f'{current_app.config["UPLOAD_PATH"]}/{O0O00OO0OOO000O00}'
                O000O0OO0000OO00O =f'{O0O00OOOOO0OO0OOO}{O0O00OOO0O000O000}'
                OO00O0000O00O000O =os .path .dirname (O000O0OO0000OO00O )
                O0O0OOO000O00OOOO =''
                if not os .path .exists (OO00O0000O00O000O ):
                    try :
                        os .makedirs (OO00O0000O00O000O )
                    except :
                        O0O0OOO000O00OOOO ='创建目录失败!'
                elif not os .access (OO00O0000O00O000O ,os .W_OK ):
                    O0O0OOO000O00OOOO ='没有写入权限!'
                if O0O0OOO000O00OOOO :jsonify ({"status":1 ,"msg":O0O0OOO000O00OOOO })
                OOO0OO0O000O0O0OO .save (O000O0OO0000OO00O )
                _OOOO0O0OO00O00O0O =O0O00OOO0O000O000
            else :
                _OOOO0O0OO00O00O0O =qiNiu_upload (O0O00OO0OOO000O00 ,OOO0OO0O000O0O0OO .read ())
            if _OOOO0O0OO00O00O0O :return jsonify ({"status":0 ,"img_url":_OOOO0O0OO00O00O0O })
            return jsonify ({"status":1 ,"msg":'上传异常,请确认七牛云配置正确'})
        return jsonify ({"status":1 ,"msg":'上传失败,超出允许文件类型!'})
    else :
        return jsonify ({"status":1 ,"msg":'未知请求错误'})
